package com.yznote.cloudesk.file.server.impl;


import com.yznote.cloudesk.common.constants.YCacheConstant;
import com.yznote.cloudesk.common.constants.YConstant;
import com.yznote.cloudesk.common.exception.YException;
import com.yznote.cloudesk.common.pojo.File;
import com.yznote.cloudesk.common.pojo.Garbage;
import com.yznote.cloudesk.common.tokenShare.YTokenShare;
import com.yznote.cloudesk.common.oss.OssUtils;
import com.yznote.cloudesk.cache.yCache.YCache;
import com.yznote.cloudesk.file.mapper.DeskMapper;
import com.yznote.cloudesk.file.mapper.FileMapper;
import com.yznote.cloudesk.file.mapper.GarbageMapper;
import com.yznote.cloudesk.file.server.FileServer;
import com.yznote.cloudesk.file.server.GarbageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author : xiaozhoutongxue
 * @date : 2022/10/20 23:21
 */
@Service
public class GarbageServerImpl implements GarbageServer {
    @Autowired
    GarbageMapper garbageMapper;
    @Autowired
    YTokenShare yTokenShare;
    @Autowired
    FileMapper fileMapper;
    @Autowired
    DeskMapper deskMapper;
    @Autowired
    OssUtils ossUtils;
    @Autowired
    FileServer fileServer;
    @Autowired
    YCache yCache;

    @Override
    public List<File> selectGarbageByTime(Long userId) {
        garbageMapper.findGarbageId(userId);
        return garbageMapper.getAllByTime(userId);
    }

    @Override
    @Transactional
    public String insertGarbage(Long fileId) {
        Long userId = yTokenShare.get().getUserId();
        Long garbageId = garbageMapper.findGarbageId(userId);
        LocalDateTime now = LocalDateTime.now();
        File file = fileMapper.getFilemsg(fileId);
        String msg = insertGarbageUtils(userId, file, true, now, garbageId);
        return msg;
    }

    @Transactional
    public String insertGarbageUtils(
            Long userId,
            File file,
            Boolean isFirst,
            LocalDateTime now,
            Long garbageId) {
        Long fileFaId = file.getFileFaId();
        Long fileId = file.getFileId();
        // 防止删除我的电脑、我的桌面和回收站
        if (fileFaId.equals(-1L) || fileId.equals(userId) || fileId.equals(garbageId)) {
            throw new YException(YConstant.ERROR_GARBAGEDATAEXC + ",fileName:" + file.getFileName(), 500);
        }
        //是否回收站下第一层
        if (isFirst) {
            garbageMapper.insertGarbage(fileId, garbageId, now, userId);
        } else {
            garbageMapper.insertGarbage(fileId, fileFaId, now, userId);
        }
        //父id等于userId说明是桌面文件
        if (file.getFileFaId().equals(userId)) {
            deskMapper.DeleteFile(fileId);
            yCache.delayDelete(YCacheConstant.DESK, userId.toString());
        }
        return YConstant.SUCCESS;
    }

    @Override
    @Transactional
    public String insertMoreGarbages(Long fileId) {
        Long userId = yTokenShare.get().getUserId();
        Long garbageId = garbageMapper.findGarbageId(userId);
        LocalDateTime now = LocalDateTime.now();
        File file = fileMapper.getFilemsg(fileId);
        String msg = insertGarbageUtils(userId, file, true, now, garbageId);
        if (msg != YConstant.SUCCESS) {
            return msg;
        }
        List<File> sonFileList = fileMapper.getSonFile(fileId, userId);
        for (int i = 0; i < sonFileList.size(); i++) {
            msg = dfsInser(userId, sonFileList.get(i), false, now, garbageId);
            if (msg != YConstant.SUCCESS) {
                return msg;
            }
        }
        return YConstant.SUCCESS;
    }

    @Transactional
    public String dfsInser(Long userId,
                           File file,
                           Boolean isFirst,
                           LocalDateTime now,
                           Long garbageId) {
        String msg = insertGarbageUtils(userId, file, isFirst, now, garbageId);
        if (msg != YConstant.SUCCESS) {
            return msg;
        }
        List<File> sonFileList = fileMapper.getSonFile(file.getFileId(), userId);
        for (int i = 0; i < sonFileList.size(); i++) {
            dfsInser(userId, sonFileList.get(i), false, now, garbageId);
        }
        return YConstant.SUCCESS;
    }

    @Override
    public void deleteGarbage(Long fileId) throws Exception {
        Garbage garbage = garbageMapper.getGarbageMsg(fileId);
        garbageMapper.deleteGarbage(fileId);
        ossUtils.deleteOss(garbage.getFilePath());
    }

    @Override
    @Transactional
    public void deleteMoreGarbages(Long fileId) throws Exception {
        Garbage garbage = garbageMapper.getGarbageMsg(fileId);
        garbageMapper.deleteGarbage(fileId);
        if (garbage.getFileType() == 1) {
            ossUtils.deleteOss(garbage.getFilePath());
        }
        List<File> sonGarbage = garbageMapper.getSonGarbage(fileId);
        for (int i = 0; i < sonGarbage.size(); i++) {
            deleteMoreGarbages(sonGarbage.get(i).getFileId());
        }
    }

    @Override
    @Transactional
    public void deleteAllGarbages() throws Exception {
        List<File> garbageList = selectGarbageByTime(yTokenShare.get().getUserId());
        for (int i = 0; i < garbageList.size(); i++) {
            if (garbageList.get(i).getFileType() == 0) {
                deleteMoreGarbages(garbageList.get(i).getFileId());
            } else {
                deleteGarbage(garbageList.get(i).getFileId());
            }
        }
    }


}
